// 载入页组件
import { _decorator } from 'cc';
import { CCVMParentComp } from '../common/ccvmParentComp'
import {ecs} from '../framework/assets/libs/ecs/ECS';
import {oops} from '../framework/assets/core/Oops';
import { UIPrefabType } from '../config/config';
import {resLoader} from "db://assets/script/framework/assets/core/common/loader/ResLoader";

const { ccclass } = _decorator;

@ccclass('LoadingComp')
@ecs.register('LoadingComp', false)
export class LoadingComp extends CCVMParentComp {
    // 载入页中的 vm 组件数据 （data 名字是写死的）
    data: any = {
        // 加载资源当前进度
        finished: 0,
        // 加载资源最大进度
        total: 0,
        // 加载进度比例
        progress: '0',
        // 加载时的提示信息
        prompt: '',
    }

    // 带小数的进度
    private progress: number = 0;

    reset(): void {
        console.log('open main prefab');
        oops.gui.remove(UIPrefabType.loading);
    }

    start() {
        console.log('loading loading');
        // this.addEvent();
        this.loadResource();
    }

    // 加载 cell 等等
    private loadResource() {
        resLoader.loadDir('cells', this.onLoadingProgress.bind(this), this.onLoadingSuccess.bind(this));
    }

    private onLoadingProgress(finished: number, total: number) {
        this.data.finished = finished;
        this.data.total = total;
        this.data.progress = (finished / total * 100).toFixed(2);
    }

    // private addEvent() {
    //     this.on(Event.loadingSuccess, this.onLoadingSuccess, this);
    // }

    private onLoadingSuccess() {
        // 触发 this.reset()
        this.ent.remove(LoadingComp);
        // 打开主界面
        oops.gui.open(UIPrefabType.game);
    }
}
